﻿define(function (require, exports, module) {
    var Config = require('config');
    var Canvas = require('canvas');
    var ImageStorage = require('imageStorage');
    var Request = require('request');
    var Map = require('map');

    var m_bgLayer;
    var m_progressLayer = Canvas.createLayer();
    var m_progressX = 1;
    var m_progressY = 559;
    var m_progressWidth = 798;
    var m_progressHeight = 40;
    var m_progress;
    var m_progressText;

    exports.start = function () {
        Canvas.clear();
        m_bgLayer = Canvas.createLayer();
        m_progressLayer = Canvas.createLayer();
        if (Config.getIsInitialize()) {
            //开始游戏
        }
        else {
            m_loadingAsync().start();
        }
    };

    var m_loadingAsync = eval(Wind.compile('async', function () {
        $await(m_drawInterfaceAsync());
        //加载配置
        var getInitializationTask = Request.ajaxAsync({
            url: 'getInitializationData.do',
            data: { width: $(window).width(), height: $(window).height() }
        });
        getInitializationTask.start();
        var index = 0, max = 20;
        while (getInitializationTask.status == 'running') {
            index = index < max ? (index + 1) : max;
            drawProgress(index);
            $await(Wind.Async.sleep(200));
        };

        var response = getInitializationTask.result;
        if (!response.Success) {
            drawLoadingFailure(response.Message);
            return;
        }
        drawProgress(max);

        //加载图片
        for (var i = 0, src; src = response.Data.Images[i]; i++) {
            var success = $await(ImageStorage.addAsync(src));
            if (success) {
                drawProgress(max + (i + 1) / response.Data.Images.length * (100 - max));
            }
            else {
                drawLoadingFailure('图片下载失败,请检查您的网速是否顺畅!');
                return;
            }
        }
        Config.init(response.Data.Config);
        $await(Wind.Async.sleep(300));
        Canvas.resize();

        console.log(response.Data);
        Map.init(response.Data);
    }));

    var m_drawInterfaceAsync = eval(Wind.compile('async', function () {
        m_progress = Canvas.createRect({
            fill: 'lightblue',
            x: m_progressX,
            y: m_progressY,
            width: 0,
            height: m_progressHeight
        });
        m_progressText = Canvas.createText({
            fontSize: 20,
            x: 300,
            y: m_progressY + 10,
            width: 200,
            height: m_progressHeight,
            align: 'center',
            fill: 'red'
        });
        m_progressLayer.add(m_progress);
        m_progressLayer.add(m_progressText);

        Canvas.add(m_bgLayer);
        Canvas.add(m_progressLayer);

        var img = $await(ImageStorage.getAsync('images/other/loading.jpg'));
        var context = m_bgLayer.getContext();
        context.drawImage(img, 0, 0);
        context.strokeRect(m_progressX, m_progressY, m_progressWidth, m_progressHeight);
    }));

    function drawProgress(percent) {
        m_progress.setWidth(percent / 100 * m_progressWidth);
        m_progressLayer.draw();
    };

    function drawLoadingFailure(msg) {
        m_progressText.setText(msg);
        m_progressLayer.draw();
    };
});
//define(function (require, exports, module) {
//    var Canvas = require('canvas');
//    var Config = require('config');
//    var Map = require('map');
//    var Player = require('player');
//    var ImageStorage = require('imageStorage');
//    var Request = require('request');
//    var m_loadingCanvas = Canvas.Get();
//    var m_progressX = 1;
//    var m_progressY = 559;
//    var m_progressWidth = 798;
//    var m_progressHeight = 40;

//    exports.Start = eval(Wind.compile('async', function () {
//        $await(InitializationAsync());

//        Canvas.Init();

//        Map.Show();
//        Player.Show();
//        require('chat').Show();
//    }));

//    var InitializationAsync = eval(Wind.compile('async', function () {
//        var img = $await(ImageStorage.GetAsync('images/other/loading.jpg'));
//        m_loadingCanvas.Call('drawImage', img, 0, 0);
//        m_loadingCanvas.Call('strokeRect', m_progressX, m_progressY, m_progressWidth, m_progressHeight);

//        //加载配置
//        var progress = 10;
//        var response = $await(Request.AjaxAsync({
//            url: 'getConfig.do',
//            data: { width: $(window).width(), height: $(window).height() }
//        }));
//        if (!response.Success) {
//            ShowLoadingError(response.Message);
//            return;
//        }
//        Config.Init(response.Data);
//        ShowLoadingProgress(progress);

//        //加载地图
//        var loadMapAsync = Request.AjaxAsync({ url: 'getMapResources.do' });
//        loadMapAsync.start();
//        for (var i = 0; i < 30; i++) {
//            progress += 2;
//            ShowLoadingProgress(progress);
//            $await(Wind.Async.sleep(100));
//        }
//        response = $await(loadMapAsync);
//        progress = 80;
//        if (!response.Success) {
//            ShowLoadingError(response.Message);
//            return;
//        }
//        Map.Init(response.Data);
//        ShowLoadingProgress(progress);

//        //加载玩家
//        response = $await(Request.AjaxAsync({ url: 'getPlayer.do' }));
//        if (!response.Success) {
//            ShowLoadingError(response.Message);
//            return;
//        }
//        Player.Init(response.Data);
//        ShowLoadingProgress(100);
//    }));

//    function ShowLoadingProgress(progress) {
//        m_loadingCanvas.Call('save');
//        m_loadingCanvas.Set('fillStyle', 'lightblue');
//        m_loadingCanvas.Call('fillRect', m_progressX, m_progressY, m_progressWidth / 100 * progress, m_progressHeight);
//        m_loadingCanvas.Call('restore');
//    };

//    function ShowLoadingError(msg) {
//        m_loadingCanvas.Call('save');
//        m_loadingCanvas.Set('fillStyle', 'red')
//                .Set('textAlign', 'center')
//                .Set('textBaseline', 'top');
//        m_loadingCanvas.ChangeFont(20, true);
//        m_loadingCanvas.Call('fillText', msg, Config.GetWidth() / 2, m_progressY);
//        m_loadingCanvas.Call('restore');
//    };

//    exports.End = function () {
//        window.open('', '_self');
//        window.opener = null;
//        window.close();
//    };
//});